﻿using System;
using System.Collections.Generic;
using System.Linq;
using Eisk.BusinessEntities;
using Eisk.BusinessLogicLayer;
using Eisk.Helpers;
namespace Eisk.Web
{
    public partial class view_employees : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Employee> employeeList = new List<Employee>();
                
                if (!string.IsNullOrEmpty(Request.QueryString["supplierId"]))
                {
                    string supplierID = Request.QueryString["supplierId"];
                    int supplierId = Convert.ToInt16(supplierID);

                    if (supplierId > 0)
                    {
                        try
                        {
                            List<Employee> employeeAllList = new EmployeeBLL().GetAllEmployees();

                            List<SupplierEmployee> supplierEmployeeList = new SupplierEmployeeBLL().GetSupplierEmployeesBySupplierId(supplierId);

                            List<Int32> employeeIdList = new List<Int32>();

                            foreach (SupplierEmployee item in supplierEmployeeList)
                            {
                                Int32 intData = (int)item.EmployeeId;

                                employeeIdList.Add(intData);
                            }

                            employeeIdList = employeeIdList.Distinct().ToList();

                            Employee aEmployee = null;

                            foreach (Int32 item in employeeIdList)
                            {
                                aEmployee = employeeAllList.FirstOrDefault(x => x.EmployeeId == item);

                                if (aEmployee != null)
                                {
                                    employeeList.Add(aEmployee);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ltlMessage.Text += String.Format(MessageFormatter.GetFormattedErrorMessage("Suppliers not found for. Supplier Id {0}"), supplierId);
                            gridViewEmployees.Visible = false;
                        }
                    }
                }

                gridViewEmployees.DataSource = employeeList;
                gridViewEmployees.DataBind();
            }
        }

        protected void LinkButtonViewSuppliers_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("view-suppliers.aspx");
        }
    }
}